package com.shanshan.manage.web.utils;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.shanshan.manage.web.domain.ACustomsMsg;

public class ACustomsMsgORCUtil {

    public static ACustomsMsg ACustomsMsgORC(String msg){
        ACustomsMsg aCustomsMsg = new ACustomsMsg();

        JSONObject jsonObject = JSONObject.parseObject(msg);
        JSONObject body1 = jsonObject.getJSONObject("body");
        JSONObject data1 = body1.getJSONObject("data");
        JSONArray prismTablesInfo = data1.getJSONArray("prism_tablesInfo");
        String content = data1.get("content").toString();
        String s = content.substring(content.indexOf("预录入编号")+6, content.indexOf("海关编号")).trim();
        aCustomsMsg.setCustomsNum(s);//报关单号
        JSONObject list = (JSONObject) prismTablesInfo.get(0);
        JSONArray cellInfos = list.getJSONArray("cellInfos");
        for (Object cellInfo : cellInfos) {
            JSONObject from = JSONObject.from(cellInfo);
            String word = from.get("word").toString();
            if (word.contains("境内收货人")){
                aCustomsMsg.setOperateCompany(word.substring(word.indexOf(")")+1).trim());
            }else if (word.contains("进口日期")||word.contains("进口期")){
                int i = word.indexOf("期")+1;
                String comDate = word.substring(i).trim();
                DateTime parse = DateUtil.parse(comDate);
                aCustomsMsg.setComeDate(parse);
            }else if (word.contains("申报日期")||word.contains("申报期")){
                int i = word.indexOf("期")+1;
                String reportDate = word.substring(i).trim();
                DateTime parse = DateUtil.parse(reportDate);
                aCustomsMsg.setReportDate(parse);
            }else if (word.contains("运输工具名称及航次号")){
                String cmhc = word.substring(word.indexOf("号")+1);
                String[] split = cmhc.split("/");
                aCustomsMsg.setShipName(split[0]);
                aCustomsMsg.setFlight(split[1]);
            }else if (word.contains("提运单号")){
                aCustomsMsg.setBillLadingNum(word.substring(word.indexOf("号")+1).trim().replaceAll(" ",""));
            }else if (word.contains("件数")){
                aCustomsMsg.setPieces(word.substring(word.indexOf("数")+1).trim());
            }else if (word.contains("毛重")){
                String trim = word.substring(word.indexOf(")")+1).trim();
                aCustomsMsg.setGrossWeight(Double.parseDouble(trim));
            }else if (word.contains("净重")){
                String trim = word.substring(word.indexOf(")")+1).trim();
                aCustomsMsg.setNetWeight(Double.parseDouble(trim));
            }else if (word.contains("启运国")){
                aCustomsMsg.setCountryDeparture(word.substring(word.indexOf(")")+1).trim());
            }else if (word.contains("贸易国")){
                aCustomsMsg.setTradingNations(word.substring(word.indexOf(")")+1).trim());
            }else if (word.contains("合同协议号")){
                aCustomsMsg.setContractAgreement(word.substring(word.indexOf("号")+1).trim());
            }
        }

        return aCustomsMsg;
    }


}
